package com.feihong.yw189.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.PhoneUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.feihong.yw189.activity.base.BaseActivity;
import com.feihong.yw189.app.App;
import com.feihong.yw189.global.GlobalConfig;
import com.feihong.yw189.message.LoginMessage;

import java.security.MessageDigest;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * Created by PVer on 2017/12/6.
 * 手机信息获取
 */

public class MyLoginUtils {
    //public Context context;

    private String user_name = "";
    private String user_password = "";
    private String version = "";
    private String phone_os = "";
    private String phone_type = "";
    private String phone_brand = "";
    private String phone_imei = "";
    private String phone_gps = "";
    private String Network = "";
    private String Phone_num = "";


    private String sign = "";

    public MyLoginUtils() {
    }

    public MyLoginUtils(Context context) {
        // this.context = App.getApp();
    }

    //
    public String getLoginSign() {
        sign = GlobalConfig.PUBLIC_KEY
                + "user_name" + user_name
                + "user_password" + user_password
                + "version" + version
                + "phone_os" + phone_os
                + "phone_type" + phone_type
                + "phone_brand" + phone_brand
                + "phone_imei" + phone_imei
                + "phone_gps" + phone_gps
                + "Network" + Network
                + "Phone_num" + Phone_num;
        try {
            encode(sign);
        } catch (Exception e) {
        }

        return sign;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public void setUser_password(String user_password) {
        this.user_password = user_password;
    }

    public String getUser_name() {
        return user_name;
    }

    public String getUser_password() {
        return user_password;
    }


    public String getVersion() {
        version = AppUtils.getAppVersionName();
        if (version == null) {
            version = "";
        }
        return version;
    }

    /**
     * @return 手机系统 phone_os:  23
     */
    public String getPhone_os() {
        phone_os = "" + DeviceUtils.getSDKVersionName();
        if (phone_os == null) {
            phone_os = "";
        }
        return phone_os;
    }

    /**
     * @return 手机型号
     */
    public String getPhone_type() {
        phone_type = DeviceUtils.getModel();
        if (phone_type == null) {
            phone_type = "";
        }
        return phone_type;
    }

    /**
     * @return 手机品牌
     */
    public String getPhone_brand() {
        phone_brand = DeviceUtils.getManufacturer();
        if (phone_brand == null) {
            phone_brand = "";
        }
        return phone_brand;
    }

    /**
     * @return 手机IMEI
     */
    public String getPhone_imei() {
        if (ActivityCompat.checkSelfPermission(App.getApp().getApplication(), Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            phone_imei = PhoneUtils.getIMEI();
        }

        if (phone_imei == null) {
            phone_imei = "";
        }
        return phone_imei;
    }

    public void setPhone_gps(String phone_gps) {
        this.phone_gps = phone_gps;
    }

    /**
     * @return gprs定位, 先写死
     */
    public String getPhone_gps() {
        phone_gps = 0.0 + ":" + 0.0;
        if (phone_gps == null) {
            phone_gps = "";
        }
        return phone_gps;
    }

    /**
     * NETWORK_WIFI,
     * NETWORK_4G,
     * NETWORK_3G,
     * NETWORK_2G,
     * NETWORK_UNKNOWN,
     * NETWORK_NO
     *
     * @return 获取网络状态
     */
    public String getNetwork() {
        Network = NetworkUtils.getNetworkType().toString();
        if (Network == null) {
            Network = "";
        }
        return Network;
    }

    /**
     * 暂时不用
     *
     * @return 获取手机号码
     */
    public String getPhone_num() {
        if (Phone_num == null) {
            Phone_num = "";
        }
        return Phone_num;
    }

    public static String encode(String string) {
        try {
            byte[] hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
            StringBuilder hex = new StringBuilder(hash.length * 2);
            for (byte b : hash) {
                if ((b & 0xFF) < 0x10) {
                    hex.append("0");
                }
                hex.append(Integer.toHexString(b & 0xFF));
            }

            return hex.toString();
        } catch (Exception e) {
            LogUtils.e("loginUtils" + "encode---err: " + e);
            return "encode---err";
        }
    }

    public static Long getCuurrentTime() {
        Long time = TimeUtils.getNowMills() / 1000;

        return time;
    }
}
